import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/card_info_entity.dart';
import 'package:nyh_motorcade_app/entity/driver_details_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CardAccountInfoController extends GetxController {
  RefreshController refreshController = RefreshController();
  MultiState multiState = MultiState.loading;
  String driverId = "";
  List<CardInfoItem> cardList = [];
  ValueChanged<bool>? onUpdateEnableEdit;

  void initData(String driverId, List<CardInfoItem> cardList, ValueChanged<bool>? onUpdateEnableEdit) {
    this.driverId = driverId;
    this.cardList = cardList;
    this.onUpdateEnableEdit = onUpdateEnableEdit;
    multiState = this.cardList.isNotEmpty ? MultiState.success : MultiState.empty;
    update();
  }

  Future<void> onRefresh() async {
    getCardAccountInfo();
  }

  void onRetry() {
    multiState = MultiState.loading;
    update();
    getCardAccountInfo();
  }

  void getCardAccountInfo() {
    httpManager.post(
      Get.context,
      HttpAddress.GET_DRIVER_DETAILS,
      {"id": driverId},
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          if (refreshController.isRefresh) {
            refreshController.refreshFailed();
          }
          multiState = MultiState.fail;
          update();
          return;
        }
        DriverDetailsEntity entity = DriverDetailsEntity().fromJson(data);
        cardList = entity.cardList ?? [];
        if (refreshController.isRefresh) {
          refreshController.refreshCompleted();
        }
        multiState = cardList.isNotEmpty ? MultiState.success : MultiState.empty;
        update();
      },
      onFail: (code, msg) {
        if (refreshController.isRefresh) {
          refreshController.refreshFailed();
        }
        multiState = MultiState.fail;
        update();
      },
      onError: (msg) {
        if (refreshController.isRefresh) {
          refreshController.refreshFailed();
        }
        multiState = MultiState.error;
        update();
      },
    );
  }

  Future<void> goToBindCardPage() async {
    dynamic data = await Get.toNamed(Routes.bindCard, arguments: driverId);
    if (data != null) {
      multiState = MultiState.loading;
      update();
      getCardAccountInfo();
      //不可编辑卡账户扣款
      if (onUpdateEnableEdit != null) {
        onUpdateEnableEdit!(false);
      }
    }
  }

  void goToCardDetails(String? id) {
    Get.toNamed(Routes.cardDetails, arguments: id);
  }

  Future<void> goToRapidTransferPage(String? id, String? name, double? amount) async {
    dynamic data = await Get.toNamed(Routes.rapidTransfer, arguments: {
      Constant.PARAM_RAPID_TRANSFER_FROM: Constant.RAPID_TRANSFER_FROM_CARD,
      Constant.PARAM_ACCOUNT_ID: id,
      Constant.PARAM_ACCOUNT_NAME: name ?? "",
      Constant.PARAM_ACCOUNT_AMOUNT: amount ?? 0.0,
    });
    if (data != null) {
      multiState = MultiState.loading;
      update();
      getCardAccountInfo();
    }
  }

  void unboundCard(String? cardId) {
    httpManager.post(
      Get.context,
      HttpAddress.UNBOUND_DRIVER_CARD,
      {"driverId": driverId, "cardId": cardId},
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        if (data is bool) {
          if (!data) {
            ToastUtils.toast("解绑失败");
            return;
          }
          ToastUtils.toast("解绑成功");
          cardList.removeWhere((element) {
            return element.id == cardId;
          });
          multiState = cardList.isNotEmpty ? MultiState.success : MultiState.empty;
          update();
          //可编辑卡账户扣款
          if (cardList.isEmpty && onUpdateEnableEdit != null) {
            onUpdateEnableEdit!(true);
          }
        }
      },
    );
  }
}
